package 二分查找;

public class test3 {
    public int searchInsert(int[] nums, int target){
        // 最终left的位置就是目标位置
        int left=0,right=nums.length-1;
        if(nums.length==0) return -1;
        while (left<right){
            int mid = left+(right-left)/2;
            if(nums[mid]<target) left=mid+1;
            else right=mid;
        }
        if(nums[left]<target) return left+1;
        return left;
    }

    public int searchInsert1(int[] nums, int target){
        // 最终right的位置就是目标位置
        int left=0,right=nums.length-1;
        if(nums.length==0) return -1;
        while (left<right){
            int mid = left+(right-left+1)/2;
            if(nums[mid]>target) right=mid-1;
            else left=mid;
        }
        if(nums[right]<target) return right+1;
        return right;
    }
}
